/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import abstractmodel.InfoValue;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fightfxj
 */
public class UserInfo implements abstractmodel.IInfo{
    
    private String userName;        // 用户名 用户名应为一个合法的邮箱地址
    private String code;                //密码 密码为6 位及以上字符
    private String chineseName;     //客户姓名 中文或英文的用户姓名，默认为邮
    private String sex;             // 性别 男/女 
    private String telNumber;    //联系电话 联系电话为10 位以内的数
    public static String[] titles = new String[]{"用户名", "密码", "客户姓名", "性别", "联系电话"};
    private int id;

    public UserInfo() {
       userName = new String();
       code = new String();
       chineseName = new String();
       sex = new String("男");
       telNumber = new String();
    }
    
    public String getChineseName() {
        return chineseName;
    }

    public String getCode() {
        return code;
    }

    public String getSex() {
        return sex;
    }

    public String getTelNumber() {
        return telNumber;
    }

    public String getUserName() {
        return userName;
    }

    @Override
    public String[] getTitles() {
        return UserInfo.titles;
    }

    @Override
    public InfoValue getValue(String title) {
        InfoValue iv = new InfoValue();
        iv.setType(this.getClass());
        if (title.equals("用户名")) {
            iv.setObj(this.userName);
            iv.setType(String.class);
        }
        else if (title.equals("密码")) {
            iv.setObj(this.code);
            iv.setType(String.class);
        }
        else if (title.equals("客户姓名")) {
            iv.setObj(this.chineseName);
            iv.setType(String.class);
        }
        else if (title.equals("性别")) {
            iv.setObj(this.sex);
            iv.setType(String.class);
        }
        else if (title.equals("联系电话")) {
            iv.setObj(this.telNumber);
            iv.setType(String.class);
        }
        else if (title.equalsIgnoreCase("id")) {
            iv.setObj(new Integer(this.id).toString());
            iv.setType(String.class);
        }          
        return iv;
    }

    @Override
    public boolean setValue(String title, Object value) {
        if(value==null)
            return true;
        if (title.equals("用户名")) {
            this.userName = value.toString();
        }
        else if (title.equals("密码")) {
            try {
                //should save md5 password in the User
                String password = (String) (value);
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(password.getBytes());
                MessageDigest change = (MessageDigest) md.clone();
                byte[] changeBytes = change.digest();
                String md5Password = new String(Arrays.toString(changeBytes));
                System.out.println("pw" + value.toString());
                this.code = md5Password;
                Map loginmap=ModelFactory.data.loadLogInInfo();
                loginmap.put(this.userName, md5Password);
                ModelFactory.data.saveLogInInfo(loginmap);
            } catch (CloneNotSupportedException ex) {
                Logger.getLogger(UserInfo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(UserInfo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if (title.equals("客户姓名")) {
            this.chineseName = value.toString();
        }
        else if (title.equals("性别")) {
            this.sex = value.toString();
        }
        else if (title.equals("联系电话")) {
            this.telNumber = value.toString();
        }
        ModelFactory.data.saveUser(ModelFactory.ur);
        return true;
    }

    @Override
    public int getid() {
        return this.id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int getType() {
        return configure.Global.USER_INFO_TYPE;
    }

}
